<?php
namespace app\manage\controller;
use think\Db;
use app\manage\model\Cases as CasesModel;

class Cases extends Common
{
    public function index($tab = '1', $id = 0)
    {

            $casesModel = new CasesModel();
            $casesList = $casesModel->select();
            $this->assign('casesList', $casesList);
            //编辑菜单
            if (3 == $tab) {
                $cases_info = Db::name('cases')->where('id', $id)->find();
                if ($cases_info != null && is_array($cases_info)) {
                    $this->assign('cases_info', $cases_info);
                }
            }
        return view();
    }

    //新增
    public function add()
    {
        if (request()->isPost()) {
            $data = input('post.');
            $casesModel = new CasesModel();
            if ($casesModel->save($data)) {
                return success('添加成功', url('index', ['tab' => 1]));
            } else {
                return error('添加失败');
            }
        }
    }

    //修改信息
    public function edit()
    {
        if (request()->isPost()) {
            $data = input('post.');
            $casesModel = new CasesModel();
            if ($casesModel->allowField(true)->isUpdate()->save($data)) {
                return success('编辑成功!', url('index', ['tab' => 1]));
            } else {
                return error('编辑失败或者是未修改!');
            }
        }
    }
    //删除
    public function delete($id){
        $casesModel = new CasesModel();
        $cases = $casesModel::destroy($id);
        if ($cases) {
            return success('删除成功！', url('index', ['tab' => 1]));
        } else {
            return error('删除失败！');
        }
    }

}
